const mongoose = require('mongoose')
mongoose.connect("mongodb://127.0.0.1:27017/useApp")
    .then(() => {
        console.log("数据库连接成功");

    }).catch((err) => {
        console.log("数据库连接失败，原因-->", err);

    })
const userSchema = new mongoose.Schema({
    userName: String,
    pwd: String,
    phone: String,
    email: String,
    avatar: {
        type: String,
        default: 'https://example.com/default-avatar.png'
    },
    createdAt: {
        type: Date,
        default: Date.now
    }
});

// 密码加密中间件
userSchema.pre('save', async function (next) {
    if (this.isModified('pwd')) {
        this.pwd = await bcrypt.hash(this.pwd, 10);
    }
    next();
});
const pubSchema = new mongoose.Schema({
    title: String,
    adver:String,
    img:String,
    content: String, 
    check:{
        type:Boolean,
        default:false
    },
    sort:Number,
    status:{
        type:Boolean,
        default:false
    }
   
})
const userModel = mongoose.model("user", userSchema)
const pubModel = mongoose.model("pub", pubSchema)
module.exports={ userModel, pubModel }